{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基本使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<html>\n",
      " <body>\n",
      "  <p>\n",
      "   html&gt;\n",
      "  </p>\n",
      "  <title>\n",
      "   thie doment story\n",
      "  </title>\n",
      "  <p class=\"title\" name=\"dromouse\">\n",
      "   the domouse's story\n",
      "  </p>\n",
      "  <p class=\"title\" name=\"dromouse\">\n",
      "   the domouse's story\n",
      "  </p>\n",
      " </body>\n",
      "</html>\n",
      "thie doment story\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "html = '''html><head><title>thie doment story\n",
    "<body>\n",
    "<p class=\"title\" name='dromouse'>the domouse's story</p>\n",
    "<p class=\"title\" name='dromouse'>the domouse's story</p>\n",
    "'''\n",
    "soup = BeautifulSoup(html, 'lxml')\n",
    "print(soup.prettify())\n",
    "print(soup.title.string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<head><title>thie doment story\n",
      "</title></head> \n",
      " <class 'bs4.element.Tag'>\n",
      "title\n",
      "<p class=\"title\" name=\"dromouse\"><b>the domouse's story</b></p>\n"
     ]
    }
   ],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "html = '''<html><head><title>thie doment story\n",
    "<body>\n",
    "<p class=\"title\" name='dromouse'><b>the domouse's story</b></p>\n",
    "<p class=\"story\">Once upon a time threr were\n",
    "<a href=\"http://httpbin.org/get\" class=\"sister\" id=\"link1\"><!--Else other --></a>,\n",
    "<a href=\"http://httpbin.org/post\" class=\"sister\" id=\"link2\"> Lacle</a>and \n",
    "<a href=\"http://httpbin.org/cookie\" class=\"sister\" id=\"link3\">Title</a>\n",
    "</p>\n",
    "'''\n",
    "# 标签\n",
    "soup = BeautifulSoup(html, 'lxml')\n",
    "print(soup.head,'\\n', type(soup.head))\n",
    "print(soup.title.name)\n",
    "print(soup.p)\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dromouse\n",
      "http://httpbin.org/get\n",
      "http://httpbin.org/get\n"
     ]
    }
   ],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "html = '''<html><head><title>thie doment story\n",
    "<body>\n",
    "<p class=\"title\" name='dromouse'><b>the domouse's story</b></p>\n",
    "<p class=\"story\">Once upon a time threr were\n",
    "<a href=\"http://httpbin.org/get\" class=\"sister\" id=\"link1\"><!--Else other --></a>,\n",
    "<a href=\"http://httpbin.org/post\" class=\"sister\" id=\"link2\"> Lacle</a>and \n",
    "<a href=\"http://httpbin.org/cookie\" class=\"sister\" id=\"link3\">Title</a>\n",
    "</p>\n",
    "'''\n",
    "# 标签\n",
    "soup = BeautifulSoup(html, 'lxml')\n",
    "print(soup.p.attrs['name'])\n",
    "print(soup.a.attrs['href'])\n",
    "print(soup.a['href'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the domouse's story\n"
     ]
    }
   ],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "html = '''<html><head><title>thie doment story\n",
    "<body>\n",
    "<p class=\"title\" name='dromouse'><b>the domouse's story</b></p>\n",
    "<p class=\"story\">Once upon a time threr were\n",
    "<a href=\"http://httpbin.org/get\" class=\"sister\" id=\"link1\"><!--Else other --></a>,\n",
    "<a href=\"http://httpbin.org/post\" class=\"sister\" id=\"link2\"> Lacle</a>and \n",
    "<a href=\"http://httpbin.org/cookie\" class=\"sister\" id=\"link3\">Title</a>\n",
    "</p>\n",
    "'''\n",
    "# 标签\n",
    "soup = BeautifulSoup(html, 'lxml')\n",
    "print(soup.p.string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 且套选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "html = '''<html><head><title>thie doment story\n",
    "<body>\n",
    "<p class=\"title\" name='dromouse'><b>the domouse's story</b></p>\n",
    "<p class=\"story\">Once upon a time threr were\n",
    "<a href=\"http://httpbin.org/get\" class=\"sister\" id=\"link1\"><!--Else other --></a>,\n",
    "<a href=\"http://httpbin.org/post\" class=\"sister\" id=\"link2\"> Lacle</a>and \n",
    "<a href=\"http://httpbin.org/cookie\" class=\"sister\" id=\"link3\">Title</a>\n",
    "</p>\n",
    "'''\n",
    "# 标签\n",
    "soup = BeautifulSoup(html, 'lxml')\n",
    "print(soup.head.title.string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 子节点和子孙节点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Once upon a time threr were\\n', <a class=\"sister\" href=\"http://httpbin.org/get\" id=\"link1\"><!--Else other --></a>, ',\\n', <a class=\"sister\" href=\"http://httpbin.org/post\" id=\"link2\"> Lacle</a>, 'and \\n', <a class=\"sister\" href=\"http://httpbin.org/cookie\" id=\"link3\">Title</a>, '\\n']\n",
      "Once upon a time threr were\n",
      "\n",
      "<a class=\"sister\" href=\"http://httpbin.org/get\" id=\"link1\"><!--Else other --></a>\n",
      ",\n",
      "\n",
      "<a class=\"sister\" href=\"http://httpbin.org/post\" id=\"link2\"> Lacle</a>\n",
      "and \n",
      "\n",
      "<a class=\"sister\" href=\"http://httpbin.org/cookie\" id=\"link3\">Title</a>\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "html = '''<html><head><title>thie doment story\n",
    "<body>\n",
    "\n",
    "<p class=\"story\">Once upon a time threr were\n",
    "<a href=\"http://httpbin.org/get\" class=\"sister\" id=\"link1\"><!--Else other --></a>,\n",
    "<a href=\"http://httpbin.org/post\" class=\"sister\" id=\"link2\"> Lacle</a>and \n",
    "<a href=\"http://httpbin.org/cookie\" class=\"sister\" id=\"link3\">Title</a>\n",
    "</p>\n",
    "'''\n",
    "# 标签\n",
    "soup = BeautifulSoup(html, 'lxml')\n",
    "print(soup.p.contents)\n",
    "for i in soup.p.children:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 标准选择器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<p class=\"title\" name=\"dromouse\"><b>the domouse's story</b></p>, <p class=\"story\">Once upon a time threr were\n",
      "<a class=\"sister\" href=\"http://httpbin.org/get\" id=\"link1\"><!--Else other --></a>,\n",
      "<a class=\"sister\" href=\"http://httpbin.org/post\" id=\"link2\"> Lacle</a>and \n",
      "<a class=\"sister\" href=\"http://httpbin.org/cookie\" id=\"link3\">Title</a>\n",
      "</p>]\n"
     ]
    }
   ],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "html = '''<html><head><title>thie doment story\n",
    "<body>\n",
    "<p class=\"title\" name='dromouse'><b>the domouse's story</b></p>\n",
    "<p class=\"story\">Once upon a time threr were\n",
    "<a href=\"http://httpbin.org/get\" class=\"sister\" id=\"link1\"><!--Else other --></a>,\n",
    "<a href=\"http://httpbin.org/post\" class=\"sister\" id=\"link2\"> Lacle</a>and \n",
    "<a href=\"http://httpbin.org/cookie\" class=\"sister\" id=\"link3\">Title</a>\n",
    "</p>\n",
    "'''\n",
    "# 标签\n",
    "soup = BeautifulSoup(html, 'lxml')\n",
    "print(soup.find_all('p'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
